<template>
  <div class="manage">
    <el-dialog
      :visible.sync="isShow"
      :title="operateType === 'edit' ? '更改购买数量' : ' '"
    >
      <CommonForm :formLabel="operateFromLabel" :form="operateFrom">
      </CommonForm>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
    <div class="header">
      <CommonForm inline>
        <el-button type="danger" @click="delMore" icon="el-icon-delete"
          >删除购物商品</el-button
        >
      </CommonForm>
    </div>
    <CommonTable
      :tableData="tableData"
      :tableLabel="tableLabel"
      :config="config"
      @selection-change="handleSelectionChange"
      @changePage="findPage"
      @edit="editUser"
      @del="delUser"
    ></CommonTable>
  </div>
</template>

<script>
import CommonForm from "@/components/common/Form.vue";
import CommonTable from "@/components/common/Table.vue";
export default {
  components: {
    CommonForm,
    CommonTable
  },
  data() {
    return {
      isShow: false,
      ids: [],
      multipleSelection: [],
      operateType: "edit",
      operateFrom: {
        name: ""
      },

      operateFromLabel: [
        {
          model: "userName",
          label: "姓名"
        },
        {
          model: "password",
          label: "密码"
        },
        {
          model: "phone",
          label: "电话"
        },
        {
          model: "addr",
          label: "地址"
        }
      ],

      formLabel: [
        {
          model: "keyword",
          label: "管理员名称"
        }
      ],
      tableData: [],
      tableLabel: [
        { prop: "id", label: "ID" },
        {
          prop: "userName",
          label: "姓名"
        },
        {
          prop: "password",
          label: "密码"
        },
        {
          prop: "phone",
          label: "电话"
        },
        {
          prop: "addr",
          label: "地址"
        }
      ],
      config: {
        currentPage: 1,
        pageSize: 7,
        total: 2,
        loading: false,
        control: true
      }
    };
  },
  methods: {
    //无误获取参数
    findPage: function() {
      let pageRequest = {
        pageNumber: this.config.currentPage,
        pageSize: this.config.pageSize
      };
      this.config.loading = true;
      this.$http.admin
        .findPage(pageRequest)
        .then(res => {
          this.tableData = res.content;
          this.config.total = res.totalElements;
          this.config.loading = false;
        })
        .catch(error => {
          console.log(error);
        });
    },

    editUser(row) {
      this.operateType = "edit";
      this.isShow = true;
      this.operateFrom = row;
    },
    confirm() {
      this.$http.admin.update(this.operateFrom).then(res => {
        console.log(res);
        this.isShow = false;
        this.reload(); //编辑成功后需要重新刷新用户信息
      });
    },
    delUser(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http.admin.delUser(row.id).then(res => {
            console.log(res);
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delMore: function() {
      console.log(this.multipleSelection);
      for (let i = 0; i < this.multipleSelection.length; i++) {
        this.ids.push(this.multipleSelection[i].id);
      }
      if (this.ids.length === 0) {
        this.$message({
          type: "errot",
          message: "当前未选中任何用户"
        });
      } else {
        this.$http.admin
          .delMore(this.ids)
          .then(res => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.reload();
              this.ids.length = 0;
            }
          })
          .catch(err => {
            this.$errorInfo("网络连接失败");
            console.log(err);
            this.ids.length = 0;
          });
      }
    }
  },

  created() {
    //创建将数据请求发出
    this.findPage();
  },
  name: "User"
};
</script>

<style lang="scss" scoped>
@import "~@/styles/manage.scss";
</style>
